package com.github.data_manage.config

import com.github.data_manage.dao.FundDao
import com.github.data_manage.dao.MenuDao
import com.github.data_manage.dao.RoleDao
import com.github.data_manage.dao.UserDao
import com.github.data_manage.domain.po.FundDepthResearch
import com.github.data_manage.domain.po.Menu
import com.github.data_manage.domain.po.Role
import com.github.data_manage.domain.po.User
import com.github.data_manage.service.FundDeepResearchService
import com.github.data_manage.service.FundDepthResearchAttachService
import com.github.data_manage.service.FundService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.ApplicationListener
import org.springframework.context.annotation.Configuration
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.math.log

/**
 *@author CHN
 *@since 2024-08-18 16:55
 */
@Configuration
class InitConfig(
    private val menuDao: MenuDao,
    private val roleDao: RoleDao,
    private val userDao: UserDao,
    private val fundService: FundService,
) : ApplicationListener<ApplicationReadyEvent> {

    companion object {

        val logger: Logger = LoggerFactory.getLogger(InitConfig::class.java)


    }


    override fun onApplicationEvent(event: ApplicationReadyEvent) {
//        initMenu()
//        initRole()
        initUser()
        fundService.buildAllFundCode()
        logger.info("初始化目录")
        Paths.get(FUND_INTRODUCE_DIR).let {
            if (!Files.exists(it)) {
                Files.createDirectory(it)
            }
        }
        Paths.get(FUND_MONTH_REPORT_DIR).let {
            if (!Files.exists(it)) {
                Files.createDirectory(it)
            }
        }
        Paths.get(FUND_RESEARCH_DIR).let {
            if (!Files.exists(it)) {
                Files.createDirectory(it)
            }
        }
        Paths.get(FUND_TEMP_DIR).let {
            if (!Files.exists(it)) {
                Files.createDirectory(it)
            }
        }
        logger.info("初始化目录完成")

//        Thread.startVirtualThread {
//            logger.info("开始计算基金信息")
//            fundDao.findAll().forEach { fund ->
//            logger.info("开始计算基金{}年化信息", fund.fundName)
//                    try {
//                        val (yearNav, yearStandardDeviation) = fundService.fundYearInfo(fund.fundId!!)
//                        fund.yearNav = yearNav
//                        fund.yearStandardDeviation = yearStandardDeviation
//                        fundDao.save(fund)
//                    } catch (e: Exception) {
//                        logger.error("fundId is ${fund.fundId}", e)
//                    }

//        }

    }

    fun initMenu() {
        mapOf(
            Pair("Fund-Management", "基金管理"),
            Pair("Person-Management", "人员管理"),
            Pair("Company-Management", "公司管理"),
            Pair("User-Management", "用户管理"),
            Pair("13F", "13F"),
        ).forEach { (code, name) ->
            menuDao.findByCode(code).ifPresentOrElse(
                { _ ->

                },
                {
                    val menu = Menu()
                    menu.code = code
                    menu.name = name
                    menuDao.save(menu)
                }
            )

        }
    }

    fun initRole() {
        roleDao.findByName(defaultRoleName).ifPresentOrElse(
            { _ ->

            },
            {
                val role = Role()
                role.name = defaultRoleName
                role.menus = menuDao.findAll()
                roleDao.save(role)
            }
        )
    }

    fun initUser() {
        userDao.findByName(defaultUserName).ifPresentOrElse(
            { _ ->

            },
            {
                val user = User()
                user.name = defaultUserName
                user.password = "Admin#123"
                user.role = roleDao.findByName(defaultRoleName).get()
                userDao.save(user)
            }
        )
    }

}
